package com.briup.oagg.facade.web.controller.production;

import com.briup.framework.utils.web.Response;
import com.briup.oagg.facade.service.production.IProductionModelService;
import com.briup.oagg.facade.web.vm.production.PageInfoVM;
import com.briup.oagg.facade.web.vm.production.ProductionModelVM;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 生产模型管理信息前端控制器
 *
 * @author briup
 */
@Api(tags ="3.1.生产模型管理服务")
@RestController
@RequestMapping("/production/model")
public class ProductionModelController {
    @Autowired
    private IProductionModelService service;

    @ApiOperation("分页多条件查询生产模型信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modelType",value = "模型类型",allowableValues = "CLASSIFICATION,CLUSTER,REGRESSION",dataTypeClass = String.class),
            @ApiImplicitParam(name = "pageNum",value = "当前页码",defaultValue = "1",dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize",value = "每页大小",defaultValue = "5",dataTypeClass = Integer.class)
    })
    @GetMapping("/page")
    public Response<PageInfoVM<ProductionModelVM>> findByPage(String modelType,
                                                              @RequestParam Integer pageNum,
                                                              @RequestParam Integer pageSize){
        PageInfoVM<ProductionModelVM> page = service.findByPage(modelType,pageNum,pageSize);
        return Response.success(page);
    }

    @ApiOperation(value = "数据推理",notes = "上传数据文件，推理成功后，返回结果文件")
    @PostMapping
    public ResponseEntity<byte[]> predict(@RequestParam("modelId") String modelId,
                                          @RequestPart("dataFile") MultipartFile dataFile){
        byte[] cvsFile = service.predict(modelId,dataFile);
        ResponseEntity<byte[]> entity = ResponseEntity.ok()
                                            .contentType(MediaType.APPLICATION_OCTET_STREAM)
                                            .body(cvsFile);
        return entity;
    }

    @ApiOperation("根据模型编号删除生产模型")
    @DeleteMapping("{productionModelId}")
    public Response<String> deleteByModelId(@PathVariable("productionModelId") String modelId){
        service.deleteByModelId(modelId);
        return Response.success();
    }

}
